/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.ledger.je.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.redragon.framework.springmvc.ControllerSupport;
import org.apache.commons.lang.StringUtils;
import org.redragon.commons.lang.Times;
import org.redragon.framework.hibernate.data.RDDataBox;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.JsonDataUtil;
import org.redragon.framework.util.JsonUtil;
import org.redragon.framework.util.ShiroUtil;

import com.erp.common.voucher.util.FinVoucherUtil;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.finance.voucher.service.FinVoucherHeadService;
import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.ledger.account.dao.model.GLLedger;
import com.erp.ledger.account.service.GLCodeCombinationsService;
import com.erp.ledger.account.service.GLLedgerService;
import com.erp.ledger.balance.dao.model.GLBalance;
import com.erp.ledger.balance.dao.model.GLBalanceCO;
import com.erp.ledger.balance.service.GLBalanceService;
import com.erp.ledger.balance.util.GLBalanceUtil;
import com.erp.ledger.je.dao.data.DataBox;
import com.erp.ledger.je.dao.model.CloseLedgerHint;
import com.erp.ledger.je.dao.model.GLJeHead;
import com.erp.ledger.je.dao.model.GLJeHeadCO;
import com.erp.ledger.je.dao.model.GLJeLine;
import com.erp.ledger.je.dao.model.GLJeModelHead;
import com.erp.ledger.je.service.GLJeHeadService;
import com.erp.ledger.je.service.GLJeLineService;
import com.erp.ledger.je.service.GLJeModelHeadService;
import com.erp.ledger.je.service.GLJeModelLineService;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendarPeriod;
import com.erp.masterdata.calendar.dao.model.MdFinanceCalendarPeriodCO;
import com.erp.masterdata.calendar.service.MdFinanceCalendarPeriodService;
import com.erp.masterdata.common.service.MasterDataCommonService;

@Controller
@RequestMapping("/web/gLJeHead")
public class GLJeHeadWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(GLJeHeadWebController.class);
    
    //服务层注入
    @Autowired
    private GLJeHeadService gLJeHeadService;
    @Autowired
    private GLJeLineService glJeLineService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private GLJeModelHeadService glJeModelHeadService;
    @Autowired
    private GLJeModelLineService glJeModelLineService;
    @Autowired
    private GLCodeCombinationsService glCodeCombinationsService;
    @Autowired
    private MdFinanceCalendarPeriodService mdFinanceCalendarPeriodService;
    @Autowired
    private GLLedgerService glLedgerService;
    @Autowired
    private FinVoucherHeadService finVoucherHeadService;
    @Autowired
    private GLBalanceService gLBalanceService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getGLJeHeadList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2021-01-30 14:56:46
     * @author 
     * @param pages
     * @param gLJeHeadCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeHeadList")
    public String getGLJeHeadList(Pages pages, GLJeHeadCO gLJeHeadCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        
        //分页查询数据
        List<GLJeHead> gLJeHeadList = this.gLJeHeadService.getDataObjects(pages, gLJeHeadCO);
        //循环获取凭证金额
        for(GLJeHead glJeHead: gLJeHeadList) {
            glJeHead.setAmount(this.glJeLineService.getGLJeAmountByHeadCode(glJeHead.getJeHeadCode()));
        }
        
        //循环设置职员和组织信息
        for(GLJeHead glJeHead: gLJeHeadList) {
            glJeHead.setStaffName(this.hrCommonService.getHrStaff(glJeHead.getStaffCode()).getStaffName());
            glJeHead.setDepartmentName(this.hrCommonService.getHrDepartment(glJeHead.getDepartmentCode()).getDepartmentName());
        }
        
        //获取状态
        Map statusMap = DataBox.getStatusMap();
        //获取审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //获取日记账类型
        Map jeTypeMap = DataBox.getJeType();
        //获取日记账来源
        Map jeSourceMap = DataBox.getJeSource();
        //分类账
        GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
        //期间
        Pages pages1 = new Pages();
        pages1.setPage(1);
        pages1.setMax(100);
        MdFinanceCalendarPeriodCO mdFinanceCalendarPeriodCO = new MdFinanceCalendarPeriodCO();
        mdFinanceCalendarPeriodCO.setCalendarCode(glLedger.getCalendarCode());
        mdFinanceCalendarPeriodCO.setPeriodStatus("OPEN");
        List<MdFinanceCalendarPeriod> calendarPeriodList = this.mdFinanceCalendarPeriodService.getPeriodListByCalendarCode(pages1, mdFinanceCalendarPeriodCO);
        
        
        //页面属性设置
        model.addAttribute("gLJeHeadList", gLJeHeadList);
        model.addAttribute("pages", pages);
        model.addAttribute("statusMap", statusMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("jeTypeMap", jeTypeMap);
        model.addAttribute("jeSourceMap", jeSourceMap);
        model.addAttribute("calendarPeriodList", calendarPeriodList);
        
        return "basic.jsp?content=gLJe/jeList";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2021-01-30 14:56:46
     * @author 
     * @param gLJeHead
     * @param model
     * @return String
     *
     */
    @RequestMapping("getGLJeHead")
    public String getGLJeHead(GLJeHead gLJeHead, Model model) {
        List<GLJeLine> glJeLineList = new ArrayList<GLJeLine>();
        
        //查询要编辑的数据
        if(gLJeHead!=null&&gLJeHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeHead.getJeHeadCode())) {
            gLJeHead = this.gLJeHeadService.getDataObject(gLJeHead.getJeHeadId());
            //设置显示字段
            gLJeHead.setStaffName(this.hrCommonService.getHrStaff(gLJeHead.getStaffCode()).getStaffName());
            gLJeHead.setDepartmentName(this.hrCommonService.getHrDepartment(gLJeHead.getDepartmentCode()).getDepartmentName());
            //获取凭证行
            glJeLineList = this.glJeLineService.getGLJeLineListByHeadCode(gLJeHead.getJeHeadCode());
            //循环设置凭证头的dr和cr
            Double drAmount = 0D;
            Double crAmount = 0D;
            for(GLJeLine line: glJeLineList) {
                drAmount = drAmount + line.getDrAmount();
                crAmount = crAmount + line.getCrAmount();
                //设置科目
                //line.setSubjectDesc(this.masterDataCommonService.getSubjectMap().get(line.getSubjectCode()));
                line.setSubjectDesc(this.glCodeCombinationsService.getGLCodeCombinationsSegment(line.getSubjectCode()));
                line.setSubjectDescTitle(this.glCodeCombinationsService.getGLCodeCombinationsSegmentDesc(line.getSubjectCode()));
            }
            gLJeHead.setDrAmount(drAmount);
            gLJeHead.setCrAmount(crAmount);
            if(crAmount.doubleValue()==drAmount.doubleValue()) {
                gLJeHead.setAmount(new BigDecimal(drAmount));
            }else {
                gLJeHead.setAmount(new BigDecimal(0));
            }
        }else {
            //初始化默认字段
            Times time = new Times();
            gLJeHead.setJeName("人工 "+time.getDate()+" "+time.getTime());
            
            //获取当前用户职员信息
            HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
            gLJeHead.setStaffCode(staffInfo.getStaffCode());
            gLJeHead.setDepartmentCode(staffInfo.getDepartmentCode());
            gLJeHead.setStaffName(staffInfo.getStaffName());
            gLJeHead.setDepartmentName(staffInfo.getDepartmentName());
        }
        
        //获取状态
        Map statusMap = DataBox.getStatusMap();
        //获取审批状态
        Map approveStatusMap = RDDataBox.getApproveStatusMap();
        //获取日记账类型
        Map jeTypeMap = DataBox.getJeType();
        //获取日记账来源
        Map jeSourceMap = DataBox.getJeSource();
        //币种
        Map currencyTypeMap = this.datasetCommonService.getCurrencyType();
        //分类账
        GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
        //期间
        Pages pages = new Pages();
        pages.setPage(1);
        pages.setMax(100);
        MdFinanceCalendarPeriodCO mdFinanceCalendarPeriodCO = new MdFinanceCalendarPeriodCO();
        mdFinanceCalendarPeriodCO.setCalendarCode(glLedger.getCalendarCode());
        mdFinanceCalendarPeriodCO.setPeriodStatus("OPEN");
        List<MdFinanceCalendarPeriod> calendarPeriodList = this.mdFinanceCalendarPeriodService.getPeriodListByCalendarCode(pages, mdFinanceCalendarPeriodCO);
        //获取凭证模板
        List<GLJeModelHead> glJeModelHeadList = this.glJeModelHeadService.getDataObjects();
        
        //页面属性设置
        model.addAttribute("gLJeHead", gLJeHead);
        model.addAttribute("glJeLineList", glJeLineList);
        model.addAttribute("jeTypeMap", jeTypeMap);
        model.addAttribute("jeSourceMap", jeSourceMap);
        model.addAttribute("statusMap", statusMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("currencyTypeMap", currencyTypeMap);
        model.addAttribute("glLedger", glLedger);
        model.addAttribute("calendarPeriodList", calendarPeriodList);
        model.addAttribute("glJeModelHeadList", glJeModelHeadList);
        
        return "basic.jsp?content=gLJe/jeEdit";
    }
    
    
    
    /**
     * 
     * @description 异步获取要添加的凭证行
     * @date 2020年7月25日
     * @author dongbin
     * @return
     * @return String
     *
     */
    @RequestMapping("getTrModelAjax")
    public String getTrModelAjax() {
        return "gLJe/ajax/trModelAjax";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2021-01-30 14:56:46
     * @author 
     * @param gLJeHead
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editGLJeHead")
    public String editGLJeHead(@Valid GLJeHead glJeHead, BindingResult bindingResult, Integer[] jeLineId, String[] memo, String[] subjectCode, Double[] drAmount, Double[] crAmount, Model model, RedirectAttributes attr) {
        //参数校验
        Map<String, String> errorMap = this.validateParameters(bindingResult, model);
        if(errorMap.size()>0) {
            return "forward:getGLJeHead";
        }
        
        //对当前编辑的对象初始化默认的字段
        if(glJeHead.getJeHeadId()==null) {
            glJeHead.setJeHeadCode(FinVoucherUtil.incrVoucherNumberCache("JE").toString());
        }
        
        List<GLJeLine> glJeLineList = new ArrayList<GLJeLine>();
        //循环设置凭证行
        for(int a=0;a<subjectCode.length;a++) {
            GLJeLine glJeLine = new GLJeLine();
            //判断是新增还是修改
            if(jeLineId[a]==null) {
                glJeLine.setJeLineCode(SnowFlake.getConcurrentId().toString());
                glJeLine.setJeHeadCode(glJeHead.getJeHeadCode());
            }else {
                glJeLine.setJeLineId(jeLineId[a]);
            }
            
            glJeLine.setCrAmount(crAmount[a]);
            glJeLine.setDrAmount(drAmount[a]);
            glJeLine.setMemo(memo[a]);
            glJeLine.setSubjectCode(subjectCode[a]);
            
            glJeLineList.add(glJeLine);
        }
        
        //保存编辑的数据
        this.gLJeHeadService.insertOrUpdateGLJe(glJeHead, glJeLineList);
        //提示信息
        attr.addFlashAttribute("hint", "success");
        //重定向参数
        attr.addAttribute("jeHeadId", glJeHead.getJeHeadId());
        attr.addAttribute("jeHeadCode", glJeHead.getJeHeadCode());
        
        return "redirect:getGLJeHead";
    }
    
    
    
    /**
     * 
     * @description 修改日记账头状态或审批状态
     * @date 2020年7月27日
     * @author dongbin
     * @param finVoucherHead
     * @param model
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("editGLJeHeadStatus")
    public String editGLJeHeadStatus(GLJeHead glJeHead, Model model, RedirectAttributes attr) {
        if(glJeHead!=null&&glJeHead.getJeHeadId()!=null&&StringUtils.isNotBlank(glJeHead.getJeHeadCode())) {
            if(StringUtils.isNotBlank(glJeHead.getStatus())) {
                this.gLJeHeadService.updateHeadForStatus(glJeHead.getJeHeadId(), glJeHead.getStatus());
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else if(StringUtils.isNotBlank(glJeHead.getApproveStatus())) {
                this.gLJeHeadService.updateHeadForApproveStatus(glJeHead.getJeHeadId(), glJeHead.getApproveStatus());
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }
        }
        
        //重定向参数
        attr.addAttribute("jeHeadId", glJeHead.getJeHeadId());
        attr.addAttribute("jeHeadCode", glJeHead.getJeHeadCode());
        
        return "redirect:getGLJeHead";
    }
    
    
    
    /**
     * 
     * @description 删除数据
     * @date 2021-01-30 14:56:46
     * @author 
     * @param gLJeHead
     * @return String
     *
     */
    @RequestMapping("deleteGLJeHead")
    public String deleteGLJeHead(GLJeHead gLJeHead, RedirectAttributes attr) {
        //删除数据前验证数据
        if(gLJeHead!=null&&gLJeHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeHead.getJeHeadCode())) {
            if(gLJeHead.getApproveStatus().equals("UNSUBMIT")||gLJeHead.getApproveStatus().equals("REJECT")) {
                //删除数据
                this.gLJeHeadService.deleteDataObject(gLJeHead);
                //提示信息
                attr.addFlashAttribute("hint", "success");
            }else {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                attr.addFlashAttribute("alertMessage", "日记账已审核不能删除");
            }
        }
        
        return "redirect:getGLJeHeadList";
    }
    
    
    
    /**
     * 
     * @description 日记账过账到总账
     * @date 2021年2月1日
     * @author dongbin
     * @param gLJeHead
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("postJeToGL")
    public String postJeToGL(GLJeHead gLJeHead, RedirectAttributes attr) {
        if(gLJeHead!=null&&gLJeHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeHead.getJeHeadCode())) {
            //过账
            String result = this.gLJeHeadService.postJeToGL(gLJeHead.getJeHeadId());
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", result);
        }
        
        //重定向参数
        attr.addAttribute("jeHeadId", gLJeHead.getJeHeadId());
        attr.addAttribute("jeHeadCode", gLJeHead.getJeHeadCode());
        
        return "redirect:getGLJeHead";
    }
    
    
    
    /**
     * 
     * @description 批量过账（根据id）
     * @date 2021年2月3日
     * @author dongbin
     * @param gLJeHead
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("postJeToGLIds")
    public String postJeToGLIds(String headIds, RedirectAttributes attr) {
        try {
            String result = "";
            if(StringUtils.isNotBlank(headIds)) {
                //获取日记账头（根据ids重排）
                List<GLJeHead> list = this.gLJeHeadService.getGLJeHeadPostListByIds(headIds);
                //循环日记账
                for(GLJeHead glJeHead: list) {
                    //过账
                    result = this.gLJeHeadService.postJeToGL(glJeHead.getJeHeadId());
                }
            }
            
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", result);
        }catch(Exception e) {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "部分日记账过账失败，请查看日记过账状态");
        }
        
        return "redirect:getGLJeHeadList";
    }
    
    
    
    /**
     * 
     * @description 批量过账（根据期间）
     * @date 2021年2月3日
     * @author dongbin
     * @param gLJeHead
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("postJeToGLPeriodName")
    public String postJeToGLPeriodName(String periodName, RedirectAttributes attr) {
        try {
            String result = "";
            if(StringUtils.isNotBlank(periodName)) {
                //获取日记账头（根据期间重排）
                List<GLJeHead> list = this.gLJeHeadService.getGLJeHeadPostListByPeriodName(periodName);
                //循环日记账
                for(GLJeHead glJeHead: list) {
                    //过账
                    result = this.gLJeHeadService.postJeToGL(glJeHead.getJeHeadId());
                }
            }
            
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", result);
        }catch(Exception e) {
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "部分日记账过账失败，请查看日记过账状态");
        }
        
        return "redirect:getGLJeHeadList";
    }
    
    
    
    /**
     * 
     * @description 日记账冲销
     * @date 2021年2月1日
     * @author dongbin
     * @param gLJeHead
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("reverseJe")
    public String reverseJe(GLJeHead gLJeHead, RedirectAttributes attr) {
        if(gLJeHead!=null&&gLJeHead.getJeHeadId()!=null&&StringUtils.isNotBlank(gLJeHead.getJeHeadCode())) {
            //冲销
            String result = this.gLJeHeadService.reverseJe(gLJeHead.getJeHeadId(), gLJeHead.getPeriodName());
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", result);
        }
        
        //重定向参数
        attr.addAttribute("jeHeadId", gLJeHead.getJeHeadId());
        attr.addAttribute("jeHeadCode", gLJeHead.getJeHeadCode());
        
        return "redirect:getGLJeHead";
    }
    
    
    
    /**
     * 
     * @description 结账跳转页面
     * @date 2021年2月4日
     * @author dongbin
     * @param model
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("beforeCloseLedger")
    public String beforeCloseLedger(Model model, RedirectAttributes attr) {
        
        //分类账
        GLLedger glLedger = this.glLedgerService.getPrimaryGLLedgerByLegal(ShiroUtil.getLegalCode());
        //期间
        Pages pages1 = new Pages();
        pages1.setPage(1);
        pages1.setMax(100);
        MdFinanceCalendarPeriodCO mdFinanceCalendarPeriodCO = new MdFinanceCalendarPeriodCO();
        mdFinanceCalendarPeriodCO.setCalendarCode(glLedger.getCalendarCode());
        mdFinanceCalendarPeriodCO.setPeriodStatus("OPEN");
        List<MdFinanceCalendarPeriod> calendarPeriodList = this.mdFinanceCalendarPeriodService.getPeriodListByCalendarCode(pages1, mdFinanceCalendarPeriodCO);
        
        //页面属性设置
        model.addAttribute("calendarPeriodList", calendarPeriodList);
        
        return "basic.jsp?content=gLJe/closeLedger";
    }
    
    
    
    /**
     * 
     * @description 结账
     * @date 2021年2月6日
     * @author dongbin
     * @param model
     * @param attr
     * @return
     * @return String
     *
     */
    @RequestMapping("closeLedger")
    public String closeLedger(Integer periodId, Model model, RedirectAttributes attr) {
        if(periodId!=null) {
            //获取期间
            MdFinanceCalendarPeriod period = this.mdFinanceCalendarPeriodService.getDataObject(periodId);
            //关闭期间
            period.setPeriodStatus("CLOSE");
            this.mdFinanceCalendarPeriodService.updateDataObject(period);
            
            //提示信息
            attr.addFlashAttribute("hint", "hint");
            attr.addFlashAttribute("alertMessage", "期间（"+period.getPeriodName()+"）已完成结账，期间已关闭");
        }
        
        return "redirect:beforeCloseLedger";
    }
    
    
    
    /**
     * 
     * @description 结账前验证
     * @date 2021年2月6日
     * @author dongbin
     * @param periodId
     * @return
     * @return String
     *
     */
    @RequestMapping("validateCloseLedger")
    @ResponseBody
    public String validateCloseLedger(String periodId) {
        String result = null;
        String step1 = "";
        String step2 = "";
        String step3 = "";
        String step4 = "";
        String step5 = "";
        
        List<CloseLedgerHint> hintList = new ArrayList<CloseLedgerHint>();
        
        //步骤返回状态：pass,warn,refuse
        if(StringUtils.isNotBlank(periodId)) {
            //获取期间信息
            MdFinanceCalendarPeriod period = this.mdFinanceCalendarPeriodService.getDataObject(Integer.parseInt(periodId));
            
            //================================第一步验证期间
            step1 = "pass";
            if(!this.mdFinanceCalendarPeriodService.isOpenedForPeriod(Integer.parseInt(periodId))) {
                //查看当前期间是否打开
                step1 = "refuse";
                CloseLedgerHint hint = new CloseLedgerHint();
                hint.setHintMessage("当前期间（"+period.getPeriodName()+"）已关闭，结账的期间必须处于打开状态");
                hint.setHintClass("danger");
                hint.setSettleLink("web/mdFinanceCalendar/getMdFinanceCalendarList");
                hintList.add(hint);
            }
            
            if(!this.mdFinanceCalendarPeriodService.isClosedForPreviousPeriod(Integer.parseInt(periodId))) {
                //检查之前期间是否关闭
                step1 = "refuse";
                CloseLedgerHint hint = new CloseLedgerHint();
                hint.setHintMessage("期间（"+period.getPeriodName()+"）之前存在未关闭的期间，结账的期间前不能存在打开或未结账的期间");
                hint.setHintClass("danger");
                hint.setSettleLink("web/mdFinanceCalendar/getMdFinanceCalendarList");
                hintList.add(hint);
            }
            
            //================================第二步验证子分类账
            if(!step1.contentEquals("refuse")) {
                step2 = "pass";
                
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String startDate = dateFormat.format(period.getPeriodStartDate());
                String endDate = dateFormat.format(period.getPeriodEndDate());
                //获取创建未提交
                int num1 = this.finVoucherHeadService.getVoucherNumByStatus("Y", "UNSUBMIT", "N", startDate, endDate);
                if(num1>0) {
                    step2 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("子分类账存在"+num1+"条创建未提交的凭证，请提交或删除");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/finVoucherHead/getFinVoucherHeadList");
                    hintList.add(hint);
                }
                //获取提交未审批
                int num21 = this.finVoucherHeadService.getVoucherNumByStatus("Y", "SUBMIT", "N", startDate, endDate);
                int num22 = this.finVoucherHeadService.getVoucherNumByStatus("Y", "REJECT", "N", startDate, endDate);
                int num2 = num21+num22;
                if(num2>0) {
                    step2 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("子分类账存在"+num2+"条创建提交未审批的凭证，请审核或删除");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/finVoucherHead/getFinVoucherHeadList");
                    hintList.add(hint);
                }
                //获取创建未提交
                int num3 = this.finVoucherHeadService.getVoucherNumByStatus("Y", "APPROVE", "N", startDate, endDate);
                if(num3>0) {
                    step2 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("子分类账存在"+num3+"条创建审批未过账的凭证，请过账或作废");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/finVoucherHead/getFinVoucherHeadList");
                    hintList.add(hint);
                }
                
            }
            
            //================================第三步验证日记账
            if(!step1.contentEquals("refuse")&&!step2.contentEquals("refuse")) {
                step3 = "pass";
                //获取创建未提交
                int num1 = this.gLJeHeadService.getJeNumByStatus("NEW", "UNSUBMIT", "N", period.getPeriodName());
                if(num1>0) {
                    step3 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("日记账存在"+num1+"条创建未提交的凭证，请提交或删除");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/gLJeHead/getGLJeHeadList");
                    hintList.add(hint);
                }
                //获取提交未审批
                int num21 = this.gLJeHeadService.getJeNumByStatus("NEW", "SUBMIT", "N", period.getPeriodName());
                int num22 = this.gLJeHeadService.getJeNumByStatus("NEW", "REJECT", "N", period.getPeriodName());
                int num2 = num21+num22;
                if(num2>0) {
                    step3 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("日记账存在"+num2+"条创建提交未审批的凭证，请审核或删除");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/gLJeHead/getGLJeHeadList");
                    hintList.add(hint);
                }
                //获取创建未提交
                int num3 = this.gLJeHeadService.getJeNumByStatus("CONFIRM", "APPROVE", "N", period.getPeriodName());
                if(num3>0) {
                    step3 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage("日记账存在"+num3+"条创建审批未过账的凭证，请过账或作废");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/gLJeHead/getGLJeHeadList");
                    hintList.add(hint);
                }
            }
            
            //================================第四步验证试算平衡
            if(!step1.contentEquals("refuse")&&!step2.contentEquals("refuse")&&!step3.contentEquals("refuse")) {
                step4 = "pass";
                //获取期初的平衡表
                GLBalanceCO glBalanceCO = new GLBalanceCO();
                glBalanceCO.setStatus("Y");
                List<GLBalance> glBalanceList = this.gLBalanceService.getDataObjects(glBalanceCO);
                //验证平衡表
                String balanceResult = GLBalanceUtil.validateBalanceList(glBalanceList);
                if(StringUtils.isNotBlank(balanceResult)) {
                    step4 = "refuse";
                    CloseLedgerHint hint = new CloseLedgerHint();
                    hint.setHintMessage(balanceResult+"&nbsp;&nbsp;请通过试算平衡表核对");
                    hint.setHintClass("danger");
                    hint.setSettleLink("web/gLJeHead/getGLJeHeadList");
                    hintList.add(hint);
                }
            }
            
            //================================第五步验证科目对账
            if(!step1.contentEquals("refuse")&&!step2.contentEquals("refuse")&&!step3.contentEquals("refuse")&&!step4.contentEquals("refuse")) {
                step5 = "pass";
            }
            
        }
        
        //提示信息json
        String hintJson = "[]";
        if(hintList.size()>0) {
            hintJson = JsonUtil.listToJson(hintList);
        }
        
        result = "{\"step1\":\""+step1+"\",\"step2\":\""+step2+"\",\"step3\":\""+step3+"\",\"step4\":\""+step4+"\",\"step5\":\""+step5+"\",\"hints\":"+hintJson+"}";
        System.out.println(result);
        
        return result;
    }
    
}
